<?php
function getCI(){
	$CI =& get_instance();
	return $CI;
}

function getHeader(){ 
	$CI =getCI();
	$CI->load->view($CI->jCfg['theme'].'/header');
}
function getFooter(){
	$CI =getCI();
	$CI->load->view($CI->jCfg['theme'].'/footer');
}
function fSearch(){
	$CI =getCI();
	$CI->load->view($CI->jCfg['theme'].'/form-search');
}
function getTinymce(){
	$CI =getCI();
	$CI->load->view($CI->jCfg['theme'].'/tinymce');
}

function getTheme(){
	$CI =getCI();
	return $CI->jCfg['theme'];
}

function themeURL(){
	$CI =getCI();
	return base_url().APPPATH."views/".$CI->jCfg['theme']."/";
}

function pTxt($key='',$sep='-'){
	return str_replace($sep,' ', trim($key));
}

function modUri(){
	$CI =getCI();
	if($CI->jCfg['mod_rewrite'] == 0){
		return base_url().'index.php/';
	}else{
		return base_url();
	}
}

function myNum($num=0,$curr="Rp"){
	$curr2 = strtolower($curr);
	if($curr2=="rp"){
		return $curr.". ".number_format($num,0,",",".");
	}elseif($curr2=="$" || $curr2=="e"){
		return number_format($num,0,".",",")." ".$curr;
	}else{
		return $curr.". ".number_format($num,0,",",".");
	}
}

function _l($key=""){
	if(trim($key)!=='')
		return lang($key);
}

function cfg($o='app_name'){ 
	$CI =getCI();
	return $CI->config->item($o);
}

function myDate($dt,$f="d/m/Y H:i",$s=true){
	$day = array(
		1 => "Senin",
		2 => "Selasa",
		3 => "Rabu",
		4 => "Kamis",
		5 => "Jum'at",
		6 => "Sabtu",
		7 => "Minggu"
	);
	$ts = strtotime($dt);
	return ($s)?$day[date("N",$ts)].", ".date($f,$ts):date($f,$ts);
}

function debugCode($r=array(),$f=TRUE){
	echo "<pre>";
	print_r($r);
	echo "</pre>";
	
	if($f==TRUE)
		die;
}

function getCfgApp($key=''){
	$CI =getCI();
	$vl='';
	if($key!=''){
		$v=$CI->db->query("select config_value from macs_config where config_name='".dbClean($key)."' limit 1")->row();
		$vl=$v->config_value;
	}
	return $vl;
}


function mDate($date="",$v="+1 day",$format='Y-m-d'){
	$date 	= (trim($date)=="")?date("Y-m-d"):$date;
	$nd 	=  strtotime(date("Y-m-d", strtotime($date)) . $v);
	return date($format,$nd);
}

function get_image($url="",$noimage=""){
	if(trim($noimage)==""){
		$no_image = base_url()."images/no-image.jpg";
	}else{
		$no_image = themeUrl()."images/".$noimage;
	}
	$img = "";
	if(trim($url)!=""){
		$nurl = "./".str_replace(base_url(),"",$url);
		if(file_exists($nurl) && !is_dir($nurl)){
			$img = $url;
		}else
			$img = $no_image;
	}else
		$img = $no_image;
	
	return $img;
}
function getCaptcha($p=array()){
	$CI =getCI();
	echo $CI->generateCaptcha(array(
			'width'	=> isset($p['width'])?$p['width']:80,
			'height'=> isset($p['height'])?$p['height']:30
		));
}

function _ac($c='index'){
	if(trim($c)!==''){
		$CI  = getCI();
		$acc = $CI->jCfg['access'];
		if(isset($acc[$c])){
			return TRUE;
		}else
			return FALSE;
	}else{
		return FALSE;
	}
}

function getAsuransi($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select asuransi_name from tm_pialang_asuransi where asuransi_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->asuransi_name;
		else
			return "-";
	}else
		return "-";
}

function getCabang($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select cabang_name from tm_pialang_cabang where cabang_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->cabang_name;
		else
			return "-";
	}else
		return "-";
}

function getClientName($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select pa_name from tm_pialang_client_pa where pa_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->pa_name;
		else
			return "-";
	}else
		return "-";
}

function getBankName($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select bank_name from tm_bank where bank_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->bank_name;
		else
			return "-";
	}else
		return "-";
}

function get_email_asuransi($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select asuransi_email from tm_pialang_asuransi where asuransi_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->asuransi_email;
		else
			return "";
	}else
		return "";
}

function get_detail_form($par=array()){
	$CI  = getCI();
	$where = "";
	
	if( isset($par['form_id']) && trim($par['form_id']) !="" ){
		$where .=" AND a.as_form_id = '".$par['form_id']."'";
	}
	
	$sql = "
		select 
			SQL_CALC_FOUND_ROWS a.as_form_id, b.*
		FROM tm_asuransi_form a, tm_pialang_asuransi b
		WHERE a.as_asuransi_id = b.asuransi_id
			".$where."
		ORDER BY b.asuransi_name ASC 
	";	 
	
	$query = $CI->db->query($sql);		
	$found_rows = $CI->db->query('SELECT FOUND_ROWS() as found_rows');
	return array(
		"data" 	=> $query->result(),
		"count"	=> $found_rows->row()
	);
}

function get_note($par=array()){
	if(count($par)==2){
		$CI  = getCI();
		$q = $CI->db->query("
			SELECT 
				* 
			FROM tm_pialang_note 
				WHERE note_form_id = '".dbClean($par['form_id'])."'
				AND note_as_id = '".dbClean($par['asuransi_id'])."'
			order by note_id DESC
		")->result();
		return $q;
	}else{
		return array();
	}
}
function getUser($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select * from macs_user where user_id= '".dbClean($id)."'
			order by user_id DESC
		")->row();
		return $q->user_name;
	}else{
		return array();
	}
}

function getDept($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select dept_name from tm_master_dept where dept_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->dept_name;
		else
			return "-";
	}else
		return "-";
}

function getMarketing(){
	$CI  = getCI();
	return $CI->db->query("
		select * from tm_pialang_marketing order by marketing_id ASC 
	")->result();
}

function getPosition($id=""){
	if(trim($id)!=""){
		$CI  = getCI();
		$q = $CI->db->query("
			select position_desc from tm_master_position where position_id = '".dbClean($id)."'
		")->row();
		if(count($q)>0)
			return $q->position_desc;
		else
			return "-";
	}else
		return "-";
}
function getGroup($id='',$design=false){
	$CI =getCI();
	$q = "select mag.* from macs_acl_user_group maug
		  left join macs_acl_group mag on mag.id = maug.group_id 
		  where 
		  	maug.user_id = '".idClean($id)."' 
			AND maug.app_id = '".$CI->jCfg['app_id']."' 
		  limit 1";
	$sql = $CI->db->query($q)->row();
	if($design==false){
		return count($sql)>0?$sql->group_name:'';
	}else{
		return count($sql)>0?$sql->group_template:'';
	}
}


function getLinks($links=array()){
	$CI =getCI();
	$uri =  $CI->uri->segment(3);
	if(count($links)>0){
		foreach($links as $v){
			if($v['action']!="bug"){
				if(trim($uri)==''||trim($uri)=='search'||trim($uri)=='access'){
					$fc = 'index';
				}else{		
					$fc = (trim($uri)=='edit'||trim($uri)=='add')?'add':$uri;
				}
			?>
				<a href="<?php echo $v['link'];?>" class="south <?php echo ($fc==$v['action'])?'active':'';?>" title="<?php echo $v['title'];?>"><img src="<?php echo themeUrl();?>images/<?php echo $v['image'];?>" align="left" /></a>
			<?php
			}else{
			?>
				<a href="<?php echo modUri();?>bug?url=<?php echo $CI->own_link;?>" class="south simple" rel="380,300" title="<?php echo $v['title'];?>"><img src="<?php echo themeUrl();?>images/<?php echo $v['image'];?>" align="left" /></a>
			<?php
			}
		}
	}
}

function cat_search($m=array()){
	foreach($m as $k=>$val){
		foreach($val as $p=>$v){
			$s="";
			if($k==$p)
				$s="selected='selected'";	
			echo "<option value='$p' $s>".$v."</option>";
		}
	}
}

function js_validate(){
	?>
		<link rel="stylesheet" href="<?php echo base_url();?>style/validationEngine.jquery.css" type="text/css" media="screen" title="no title" charset="utf-8" />
		<script src="<?php echo base_url();?>js/jquery.validationEngine-en.js" type="text/javascript"></script>
		<script src="<?php echo base_url();?>js/jquery.validationEngine.js" type="text/javascript"></script>
		<script>	
		$(document).ready(function() {
			$("#form-validated").validationEngine();
		});
		</script>
	<?php
}

function get_style(){
	?>
	<style>
		*{font-size:12px; font-family:tahoma; padding:0px; margin:0px;}
		.tabel{ width:100%;}.tabel tr td{padding:4px;}
		h3{font-weight:normal; font-size:18px; border-bottom:1px solid #999999; padding:4px;}
		body{margin:0px; padding:0px;}
	</style>
	<?php
}

function link_action($links=array(),$id=""){
	if(count($links)>0){
		foreach($links as $m){
			$property = "";
			if($m['type']=='simple'){
				$property = " class='south simple' rel='500|400'";
			}elseif($m['type']=='confirm'){
				$property = " class='south confirm' rel='"._l('are_you_sure')."'";
			}else{
				$property = " class='south' ";	
			}	
		?>
		<a href="<?php echo $m['link']."/".$id;?>" <?php echo $property;?> title="<?php echo $m['title'];?>"><img src="<?php echo themeUrl();?>images/<?php echo $m['image'];?>" /></a>
		<?php  	
		} 
	}
}

function _ajax_cek($par=array()){
	$CI =getCI();
	
	$validateValue	= $_POST['validateValue'];
	$validateId		= $_POST['validateId'];
	$validateError	= $_POST['validateError'];
	
	$arrayToJs = array();
	$arrayToJs[0] = $validateId;
	$arrayToJs[1] = $validateError;
	
	$CI->DATA->table=$par['table'];
	$cek = $CI->DATA->_cek(array(
		$par['field'] => $validateValue
	));
	$tmp = "";
	if($cek > 0){	
		$arrayToJs[2] = "false";		
		$tmp = '{"jsonValidateReturn":'.json_encode($arrayToJs).'}';			
	}else{
		$arrayToJs[2] = "true";
		$tmp = '{"jsonValidateReturn":'.json_encode($arrayToJs).'}';			
	}
	return $tmp;
}

function _get_menu($menu=array()){
	$CI  = getCI();
	if(count($menu)<0) return array();
	$mnn="";
	foreach($menu as $mn){
		$mnx 	= preg_split("/>/",$mn['menu']);
		$count	= count($mnx);
		$t		= "\$mnn";
		for($i=0;$i<$count;$i++){
			if(($count-1)==$i)
				$t .= "[]=array('menu'=>'".$mnx[$i]."','id'=>'".$mn['id']."','class_group'=>'".$mn['group']."','group'=>'".$mn['group_controller']."','name'=>'".$mn['controller_name']."');";
			else
				$t .= "['".$mnx[$i]."']";
		}
		eval($t);
	}
	
	return $mnn;
	
}

function get_asuransi($type=""){
	if($type=="")
		die();
	$CI  = getCI();	
	return $CI->db->query("
				select a.asuransi_id,a.asuransi_name 
				from tm_pialang_asuransi a, tm_detail_asuransi b
				where a.asuransi_id = b.asuransi_id
				and b.jenis = '".$type."'
			")->result();
}

function createMenu($m,$top=false)
{
	$CI  = getCI();
	$c 	= count($m);
	$i	= 0;
	$uris = $CI->uri->segment(2);
	echo ($top)?"<ul id='nav'>":"<ul>";
	foreach($m as $k=>$v)
	{	
		if(is_array($v) && !isset($v['menu']) && !isset($v['id']) && !isset($v['name']) ){
			echo "<li>";
			echo "<a href='".$CI->own_link."' class='".trim(strtolower($k))."'>".$k."</a>";
			createMenu($v,false);
			echo "</li>";
		}
		else {
			$active = ($uris==$v['name'])?'class="active-menu"':'';
			echo "<li class='".$v['group']."'>";
			echo "<a href='".modUri().$v['group']."/".$v['name']."' $active >".$v['menu']."</a>";
			echo "</li>";
		}		
		$i++;
	
	}
	echo "</ul>";
}